package com.xindz.cn.mvptest;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.xindz.cn.mvptest.base.BaseActivity;
import com.xindz.cn.mvptest.base.BasePresenter;
import com.xindz.cn.mvptest.base.BaseView;
import com.xindz.cn.mvptest.components.main.data.StartBean;
import com.xindz.cn.mvptest.components.main.data.TranBean;
import com.xindz.cn.mvptest.components.main.view.MainFragmentFragment;
import com.xindz.cn.mvptest.components.main.view.MainViewPagerAdapter;
import com.xindz.cn.mvptest.components.main.view.TestActivity;
import com.xindz.cn.mvptest.components.product.view.ProductAddActivity;
import com.xindz.cn.mvptest.components.product.view.ProductGetActivity;
import com.xindz.cn.mvptest.components.product.view.ProductShowActivity;
import com.xindz.cn.mvptest.components.user.view.RegisterActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements TabLayout.OnTabSelectedListener {

    @BindView(R.id.tl_main)
    TabLayout tlMain;
    @BindView(R.id.vp_main)
    ViewPager vpMain;
    private String[] titles = {"MVP测试", "LeanCloud测试", "三方库测试"};
    private ArrayList<Fragment> fragLists;
    private MainViewPagerAdapter adapter;

    private MainFragmentFragment mvpFragment;
    private MainFragmentFragment leanCloudFragment;
    private MainFragmentFragment threeFragment;

    private ArrayList<StartBean> mvpList = new ArrayList<>();
    private ArrayList<StartBean> leanCloudList = new ArrayList<>();
    private ArrayList<StartBean> threeList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setNotMVP();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        mvpList.add(new StartBean("MVP+LeanCloud注册(一)", RegisterActivity.class));

        leanCloudList.add(new StartBean("商品添加", ProductAddActivity.class));
        leanCloudList.add(new StartBean("商品查询单条", ProductGetActivity.class));
        leanCloudList.add(new StartBean("商品查询列表", ProductShowActivity.class));

        threeList.add(new StartBean("Glide测试", TestActivity.class));
        initView();
    }

    @Override
    public BasePresenter createPresneter() {
        return null;
    }

    @Override
    public BaseView createView() {
        return null;
    }

    private void initView() {
        //设置tablayout标签的显示方式
        tlMain.setTabMode(TabLayout.MODE_FIXED);
        //循环注入标签
        tlMain.setTabGravity(TabLayout.GRAVITY_FILL);
        for (String tab : titles) {
            tlMain.addTab(tlMain.newTab().setText(tab));
        }
        //设置TableLayout点击事件
        tlMain.addOnTabSelectedListener(this);

        mvpFragment = MainFragmentFragment.newInstance(new TranBean(mvpList));
        leanCloudFragment = MainFragmentFragment.newInstance(new TranBean(leanCloudList));
        threeFragment = MainFragmentFragment.newInstance(new TranBean(threeList));

        fragLists = new ArrayList<>();
        fragLists.add(mvpFragment);
        fragLists.add(leanCloudFragment);
        fragLists.add(threeFragment);

        adapter = new MainViewPagerAdapter(getSupportFragmentManager(), titles, fragLists);
        vpMain.setAdapter(adapter);
        tlMain.setupWithViewPager(vpMain);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        vpMain.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
